package com.example.dellpc.myapplication;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.view.CustomPtrHeader;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrHandler2;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

/**
 * Created by 刘智强 on 2016/9/14.
 */
public class PtrFrameLayoutActivity extends Activity {
    @ViewInject(R.id.ptr_framelayout)
    private PtrFrameLayout ptrFrameLayout;
    @ViewInject(R.id.listview)
    private ListView listView;

    private String TAG = this.getClass().getName();
    private List<String> datas  = new ArrayList<>();
    private ArrayAdapter<String> adapter;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int code = msg.what;
            switch (code) {
                case 0:
                    datas.clear();
                    initDatas();
                    adapter.notifyDataSetChanged();
                    ptrFrameLayout.refreshComplete();

                    break;
                case 1:

                    ptrFrameLayout.refreshComplete();
                    break;
                default:
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptrframelayout);
        ViewUtils.inject(this);

//        PtrClassicDefaultHeader header=new PtrClassicDefaultHeader(getApplicationContext());
//        StoreHouseHeader header = new StoreHouseHeader(getApplicationContext());
        CustomPtrHeader header=new CustomPtrHeader(getApplicationContext());
//        header.initWithString("Loading...");
//        header.setTextColor(R.color.code_1);
//        header.setBackgroundColor(Color.BLUE);


        ptrFrameLayout.setHeaderView(header);
        ptrFrameLayout.addPtrUIHandler(header);
        ptrFrameLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout ptrFrameLayout) {
                Log.e(TAG, "加载更多");
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            Thread.sleep(2000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        handler.sendEmptyMessage(1);
//
//                    }
//                }).start();
                int lastIndex=datas.size();
                for (int i = 0; i < 5; i++) {
                    datas.add("i->" + (lastIndex  + i+1));
                }
                adapter.notifyDataSetChanged();
                ptrFrameLayout.refreshComplete();
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                Utils.toast(getApplicationContext(), "开始刷新！！！");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(6000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(0);

                    }
                }).start();


            }
        });
        ptrFrameLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptrFrameLayout.autoRefresh();

            }
        }, 500);
        initDatas();


        adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, datas);
        listView.setAdapter(adapter);

    }
    public  void initDatas(){
        for (int i = 0; i < 20; i++) {
            datas.add("i->" +( i+1));
        }
    }


}
